package com.ruoyi.wx.utils;

import net.sourceforge.pinyin4j.PinyinHelper;

public class PinyinUtil {
    public static String getFirstPinyinChar(String chinese) {
        if (chinese == null || chinese.isEmpty()) {
            return "#";
        }
        char firstChar = chinese.charAt(0);
        String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(firstChar);
        if (pinyinArray != null && pinyinArray.length > 0) {
            return String.valueOf(pinyinArray[0].charAt(0)).toUpperCase(); // 返回首字母大写
        }
        return String.valueOf(firstChar); // 不是中文，直接返回
    }

    public static String getPinyinFirstChar(String chinese) {
        if (chinese == null || chinese.isEmpty()) return "#";
        char firstChar = chinese.charAt(0);
        String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(firstChar);
        if (pinyinArray != null && pinyinArray.length > 0) {
            return String.valueOf(pinyinArray[0].charAt(0)).toUpperCase(); // 获取首字母
        }
        return String.valueOf(firstChar).toUpperCase(); // 非中文原样返回
    }

}

